package com.study.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.study.app.domain.AppFirmwareVersion;
import com.study.app.domain.bo.AppFirmwareVersionBo;
import com.study.app.domain.vo.AppFirmwareVersionVo;
import com.study.app.mapper.AppFirmwareVersionMapper;
import com.study.app.service.IAppFirmwareVersionService;
import com.study.common.exception.ServiceException;
import com.study.common.fdfs.ImageUtil;
import com.study.common.utils.StringUtils;
import com.study.common.core.page.TableDataInfo;
import com.study.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * app固件版本管理Service业务层处理
 *
 * @author study
 * @date 2023-11-02
 */
@RequiredArgsConstructor
@Service
public class AppFirmwareVersionServiceImpl implements IAppFirmwareVersionService {

    private final AppFirmwareVersionMapper baseMapper;
    private final ImageUtil imageUtil;

    /**
     * 查询app固件版本管理
     */
    @Override
    public AppFirmwareVersionVo queryById(Long versionId) {
        AppFirmwareVersionVo appFirmwareVersionVo = baseMapper.selectVoById(versionId);
        appFirmwareVersionVo.setVersionUrl(imageUtil.location(appFirmwareVersionVo.getVersionUrl()));
        return appFirmwareVersionVo;
    }

    /**
     * 查询app固件版本管理列表
     */
    @Override
    public TableDataInfo<AppFirmwareVersionVo> queryPageList(AppFirmwareVersionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppFirmwareVersion> lqw = buildQueryWrapper(bo);
        Page<AppFirmwareVersionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        for (AppFirmwareVersionVo record : result.getRecords()) {
            record.setVersionUrl(imageUtil.location(record.getVersionUrl()));
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询app固件版本管理列表
     */
    @Override
    public List<AppFirmwareVersionVo> queryList(AppFirmwareVersionBo bo) {
        LambdaQueryWrapper<AppFirmwareVersion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppFirmwareVersion> buildQueryWrapper(AppFirmwareVersionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppFirmwareVersion> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getVersionName()), AppFirmwareVersion::getVersionName, bo.getVersionName());
        lqw.like(StringUtils.isNotBlank(bo.getVersionNumber()), AppFirmwareVersion::getVersionNumber, bo.getVersionNumber());
        lqw.orderByDesc(AppFirmwareVersion::getIsEnable);
        return lqw;
    }

    /**
     * 新增app固件版本管理
     */
    @Override
    public Boolean insertByBo(AppFirmwareVersionBo bo) {
        AppFirmwareVersion add = BeanUtil.toBean(bo, AppFirmwareVersion.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setVersionId(add.getVersionId());
        }
        changeEnableStatus(add);
        return flag;
    }

    /**
     * 修改app固件版本管理
     */
    @Override
    public Boolean updateByBo(AppFirmwareVersionBo bo) {
        AppFirmwareVersion update = BeanUtil.toBean(bo, AppFirmwareVersion.class);
        validEntityBeforeSave(update);
        changeEnableStatus(update);
        return baseMapper.updateById(update) > 0;
    }

    private void changeEnableStatus(AppFirmwareVersion version) {
        if (version.getIsEnable().equals(AppFirmwareVersion.YES)) {
            AppFirmwareVersion firmwareVersion = new AppFirmwareVersion();
            firmwareVersion.setIsEnable(AppFirmwareVersion.NO);
            baseMapper.update(firmwareVersion, Wrappers.<AppFirmwareVersion>lambdaUpdate()
                    .eq(AppFirmwareVersion::getIsEnable, AppFirmwareVersion.YES)
                    .ne(AppFirmwareVersion::getVersionId, version.getVersionId()));
        }
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppFirmwareVersion entity) {
        //TODO 做一些数据校验,如唯一约束
        AppFirmwareVersion firmwareVersion = baseMapper.selectOne(
                Wrappers.<AppFirmwareVersion>lambdaQuery()
                        .ne(Objects.nonNull(entity.getVersionId()), AppFirmwareVersion::getVersionId, entity.getVersionId())
                        .eq(AppFirmwareVersion::getVersionNumber, entity.getVersionNumber()));
        if (firmwareVersion != null) {
            throw new ServiceException("当前固件版本编码已被使用, 请另设置版本编码");
        }
    }

    /**
     * 批量删除app固件版本管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int changeForce(AppFirmwareVersion firmwareVersion) {
        return baseMapper.updateById(firmwareVersion);
    }

    @Override
    public int changeEnable(AppFirmwareVersion firmwareVersion) {
        changeEnableStatus(firmwareVersion);
        return baseMapper.updateById(firmwareVersion);
    }

}
